package cn.xq.pethome.sysytem.department.controller;

import cn.xq.pethome.sysytem.department.domain.Department;
import cn.xq.pethome.sysytem.department.query.DepartmentQuery;
import cn.xq.pethome.sysytem.department.service.IDepartmentService;
import cn.xq.pethome.util.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@RestController
@RequestMapping("/department")
@Api(value ="department",description="部门相关的CRUD功能")
public class DepartmentController {
  @Autowired
  private IDepartmentService departmentService;

  @GetMapping("/list")
  public List<Department> findall(){
    return departmentService.findall();
  }
  @PostMapping("/querypage")
  public List<Department> findquery(@RequestBody DepartmentQuery query){
    return departmentService.findquery(query);
  }
  @PostMapping("/{id}")
  public AjaxResult findone(@PathVariable("id") Long id){
    Department findone = departmentService.findone(id);
    return AjaxResult.success().setmsg("单项查找成功").setdata(findone);
  }
  @PutMapping("/")
  public AjaxResult save(@RequestBody Department department){
    try {
      if(department.getId()!=null){
        departmentService.update(department);
        return AjaxResult.success().setmsg("修改成功！");
      }else{
        departmentService.insert(department);
        return  AjaxResult.success().setmsg("增加成功！");
      }
    } catch (Exception e) {
      e.printStackTrace();
      return AjaxResult.error().setmsg("添加或修改失败！");
    }
  }
  @DeleteMapping("{id}")
  public AjaxResult delete(@PathVariable("id") Long id){
    departmentService.delete(id);
    return AjaxResult.success().setmsg("删除成功！");
  }

}
